package cn.mrcode.fd.blog.task;

import cn.mrcode.fd.blog.buutil.BeanUtil;
import cn.mrcode.fd.blog.services.ITagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingDeque;

/**
 * 标签异步写入 task 任务
 *
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2016/1/1 12:20
 */
public class TagHanderTask implements Runnable{
    private static Logger LOGGER = LoggerFactory.getLogger(TagHanderTask.class);
    public static LinkedBlockingDeque<TagCommand> deque  = new LinkedBlockingDeque<TagCommand>();
    private static ITagService iTagService = BeanUtil.beans.getBean(ITagService.class);
    @Override
    public void run() {
        String apiName = "[标签处理task]";
        for(;;) {
            try {
                TagCommand tagCommand = deque.take();
                int operType = tagCommand.getOperType();
                switch (operType) {
                    case 1: //添加文章对标签的引用，有可能标签是新增的
                        iTagService.addRef(tagCommand);
                        break;
                    case 2: //减少标签的引用数量
                        iTagService.delRefNum(tagCommand);
                        break;
                }
            } catch (InterruptedException e) {
                LOGGER.error(String.format("%s获取标签指令出错;异常:%s", apiName, e.getMessage()));
            }
        }
    }
}
